
package edu.sandau.basic.service.impl;

import edu.sandau.basic.domain.User;
import edu.sandau.basic.domain.stumsg;
import edu.sandau.basic.domain.stumsgExample;
import edu.sandau.basic.global.GlobalEnum;
import edu.sandau.basic.mapper.stumsgMapper;
import edu.sandau.basic.service.StumsgService;
import edu.sandau.basic.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StumsgServiceImpl implements StumsgService {

    @Resource
    private stumsgMapper stumsgMapper;
    
    @Resource
    private UserService userService;

    @Override
    public GlobalEnum.ErrorCode createStumsg(stumsg stumsg) {
        try {
            // 检查用户是否存在
            String username = stumsg.getUsername();
            if (username == null || username.trim().isEmpty()) {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
            }
            
            User user = userService.getUserByUsername(username);
            if (user == null) {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
            }
            
            // 检查任务名称是否已存在（同一用户下）
            String taskName = stumsg.getMasname();
            if (taskName == null || taskName.trim().isEmpty()) {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
            }
            
            stumsgExample example = new stumsgExample();
            stumsgExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            criteria.andMasnameEqualTo(taskName);
            
            List<stumsg> existingTasks = stumsgMapper.selectByExample(example);
            if (existingTasks != null && !existingTasks.isEmpty()) {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED; // 同一用户下任务名称已存在
            }
            
            // 设置创建时间和更新时间
            Date now = new Date();
            stumsg.setCreatetime(now);
            stumsg.setUpdatetime(now);

            // 插入数据
            try {
                int result = stumsgMapper.insert(stumsg);
                
                if (result > 0) {
                    return GlobalEnum.ErrorCode.OK;
                } else {
                    return GlobalEnum.ErrorCode.UNKNOWN;
                }
            } catch (Exception e) {
                // 检查是否是唯一键约束违反异常
                if (e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException) {
                    // 这是任务名称重复的情况
                    return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
                }
                e.printStackTrace();
                return GlobalEnum.ErrorCode.UNKNOWN;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return GlobalEnum.ErrorCode.UNKNOWN;
        }
    }
    
    @Override
    public List<stumsg> getStumsgByUsername(String username) {
        try {
            // 创建查询条件
            stumsgExample example = new stumsgExample();
            stumsgExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            
            // 执行查询
            List<stumsg> list = stumsgMapper.selectByExample(example);
            
            // 返回结果
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public GlobalEnum.ErrorCode deleteStumsgByUsernameAndName(String username, String name) {
        try {
            // 创建查询条件
            stumsgExample example = new stumsgExample();
            stumsgExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            criteria.andMasnameEqualTo(name);
            
            // 执行删除
            int deleteCount = stumsgMapper.deleteByExample(example);
            
            // 返回结果
            if (deleteCount > 0) {
                return GlobalEnum.ErrorCode.OK;
            } else {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return GlobalEnum.ErrorCode.UNKNOWN;
        }
    }

    @Override
    public GlobalEnum.ErrorCode updateStumsgByUsernameAndMsgname(String username, String msgname, String massion) {
        try {
            // 创建查询条件
            stumsgExample example = new stumsgExample();
            stumsgExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            criteria.andMasnameEqualTo(msgname);
            
            // 创建更新对象
            stumsg updateRecord = new stumsg();
            updateRecord.setMassion(massion);
            updateRecord.setUpdatetime(new Date());
            
            // 执行更新
            int updateCount = stumsgMapper.updateByExampleSelective(updateRecord, example);
            
            // 返回结果
            if (updateCount > 0) {
                return GlobalEnum.ErrorCode.OK;
            } else {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return GlobalEnum.ErrorCode.UNKNOWN;
        }
    }

    @Override
    public GlobalEnum.ErrorCode updateStumsgByUsernameAndMsgname(String username, String msgname, String massion, java.util.Date massiontime) {
        try {
            // 创建查询条件
            stumsgExample example = new stumsgExample();
            stumsgExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            criteria.andMasnameEqualTo(msgname);

            // 创建更新对象
            stumsg updateRecord = new stumsg();
            updateRecord.setMassion(massion);
            updateRecord.setMassiontime(massiontime);
            updateRecord.setUpdatetime(new Date());

            // 执行更新
            int updateCount = stumsgMapper.updateByExampleSelective(updateRecord, example);

            // 返回结果
            if (updateCount > 0) {
                return GlobalEnum.ErrorCode.OK;
            } else {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return GlobalEnum.ErrorCode.UNKNOWN;
        }
    }
    
    @Override
    public GlobalEnum.ErrorCode updateStumsgByUsernameAndMsgname(String username, String oldMsgname, String newMsgname, String massion, java.util.Date massiontime) {
        try {
            // 创建查询条件
            stumsgExample example = new stumsgExample();
            stumsgExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            criteria.andMasnameEqualTo(oldMsgname);

            // 创建更新对象
            stumsg updateRecord = new stumsg();
            updateRecord.setMasname(newMsgname);
            updateRecord.setMassion(massion);
            updateRecord.setMassiontime(massiontime);
            updateRecord.setUpdatetime(new Date());

            // 执行更新
            int updateCount = stumsgMapper.updateByExampleSelective(updateRecord, example);

            // 返回结果
            if (updateCount > 0) {
                return GlobalEnum.ErrorCode.OK;
            } else {
                return GlobalEnum.ErrorCode.USER_QUERY_FAILED;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return GlobalEnum.ErrorCode.UNKNOWN;
        }
    }
}
